<%@ page contentType="application/json;charset=UTF-8" language="java" %><%--
--%><%@ page import="java.util.Collection" %><%--
--%><%@ page import="o.json.JSONFactory" %><%--
--%><%@ page import="o.json.JSONValue" %><%--
--%><%@ page import="o.json.JSONWriter" %><%--
--%><%@ page import="o.json.JSONWriter2" %><%--
--%><%@ page import="com.google.appengine.api.users.User" %><%--
--%><%@ page import="com.google.appengine.api.users.UserService" %><%--
--%><%@ page import="com.google.appengine.api.users.UserServiceFactory" %><%--
--%><%@ page import="wwiii.Game" %><%--
--%><%@ page import="wwiii.GameManager" %><%--
--%><%@ page import="wwiii.Player" %><%--
--%><%@ page import="wwiii.PlayerManager" %><%--
--%><%

	final UserService us = UserServiceFactory.getUserService();
	if (!us.isUserLoggedIn()) {
		response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
		return;
	}
	final User user = us.getCurrentUser();

	final PlayerManager pm = new PlayerManager();
	final Player player = pm.loadPlayer(user.getUserId());

	String id = request.getParameter("id");
	if (id == null || id == "") {
		response.sendError(HttpServletResponse.SC_BAD_REQUEST, "id is not valid '"+ id +"'");
		return;
	}

	final GameManager gm = new GameManager();
	final Game game = gm.loadGame(id);

	game.removePlayer(player);
	gm.storeGame(game);

	player.removeGame(game);
	pm.storePlayer(player);

	final JSONWriter writer = new JSONWriter2();
	final JSONFactory factory = new JSONFactory();
	writer.write(factory.create(game));

%><%= writer.toString() %>
